package com.eqdd.yiqidian.ui.tongxunlu;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.FriendApplyActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.bean.rebuild.FriendApplyResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.utils.DialogUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lvzhihao on 17-4-7.
 */
@DealCode(codes = CodeRule.FRIEND_APPLY)
public class FriendApplyActivity extends BaseActivity {
    private FriendApplyActivityCustom dataBinding;
    private AlertDialog rejectTip;
    private SlimAdapterEx<FriendApplyResultBean> slimAdapterEx;

    @Subscribe(codes = {CodeRule.FRIEND_APPLY}, threadMode = ThreadMode.MAIN)
    public void refreshCode() {
        refresh();
    }


    @Override
    public void initBinding() {
        RxBus.getDefault().register(this);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_friend_apply);
        initTopTitleBar(View.VISIBLE, "好友申请");
    }

    @Override
    public void initData() {
        isRxbus=true;
    }

    @Override
    public void setView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        dataBinding.recyclerView.setLayoutManager(linearLayoutManager);
        dataBinding.recyclerView.setItemAnimator(new DefaultItemAnimator());
        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> refresh());
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_friend_apply, new SlimInjector<FriendApplyResultBean>() {
            @Override
            public void onInject(FriendApplyResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_name, data.getUpname())
                        .with(R.id.iv_head, v ->
                                ImageUtil.setCircleImage(data.getIphoto(), (ImageView) v))
                        .clicked(R.id.tv_reject, v -> {
                            if (rejectTip == null) {
                                rejectTip = DialogUtil.tip(FriendApplyActivity.this, "确定要拒绝好友请求吗?", () -> {
                                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.INVITE_FRIEND_DEAL)
                                            .params("userGuid", user.getGuid())
                                            .params("friendGuid", data.getGuid())
                                            .params("type", "false")
                                            .execute(new DialogCallBack<HttpResult>(FriendApplyActivity.this) {
                                                @Override
                                                public void onSuccess(Response<HttpResult> response) {
                                                    HttpResult httpResult = response.body();
                                                    ToastUtil.showShort(httpResult.getMsg());
                                                    if (httpResult.getStatus() == 200) {
                                                        dataBinding.coolRefreshView.setRefreshing(true);
                                                    }
                                                }
                                            });

                                });
                            }
                            rejectTip.show();
                        })
                        .clicked(R.id.tv_agree, v -> {
                            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.INVITE_FRIEND_DEAL)
                                    .params("userGuid", user.getGuid())
                                    .params("friendGuid", data.getGuid())
                                    .params("type", "true")
                                    .execute(new DialogCallBack<HttpResult>(FriendApplyActivity.this) {
                                        @Override
                                        public void onSuccess(Response<HttpResult> response) {
                                            HttpResult httpResult = response.body();
                                            ToastUtil.showShort(httpResult.getMsg());
                                            if (httpResult.getStatus() == 200) {
                                                dataBinding.coolRefreshView.setRefreshing(true);
                                            }
                                        }
                                    });
                        });
                if (data.getSign() == 0 && data.getORD().equals("friend")) {
                    injector
                            .visible(R.id.tv_reject)
                            .visible(R.id.tv_agree)
                            .gone(R.id.tv_deal);
                } else if (data.getSign() == 1) {
                    injector
                            .gone(R.id.tv_reject)
                            .gone(R.id.tv_agree)
                            .visible(R.id.tv_deal)
                            .text(R.id.tv_deal, "已添加");
                } else if (data.getSign() == -1 && data.getORD().equals("friend")) {
                    injector
                            .gone(R.id.tv_reject)
                            .gone(R.id.tv_agree)
                            .visible(R.id.tv_deal)
                            .text(R.id.tv_deal, "已拒绝");
                }
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.coolRefreshView.setRefreshing(true);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
//                    Bundle bundle = new Bundle();
//                    bundle.putString(Config.GUID,slimAdapterEx.getDataItem(position).getGuid());
//                    startActivity(UserCardActivity);
                });

    }

    private void refresh() {
        OkGo.<HttpResult<List<FriendApplyResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.INVITE_FRIEND_LIST)
                .params("uid", user.getGuid())
                .execute(new JsonCallBack<HttpResult<List<FriendApplyResultBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FriendApplyResultBean>>> response) {
                        dataBinding.coolRefreshView.setRefreshing(false);
                        HttpResult<List<FriendApplyResultBean>> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            ArrayList<FriendApplyResultBean> friendApplyResultBeans = new ArrayList<>();
                            Flowable.fromIterable(httpResult.getItems())
                                    .filter(friendApplyResultBean -> friendApplyResultBean.getORD().equals("friend"))
                                    .subscribe(friendApplyResultBean -> friendApplyResultBeans.add(friendApplyResultBean),
                                            System.out::print,
                                            () -> slimAdapterEx.updateData(friendApplyResultBeans));
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<List<FriendApplyResultBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);

        super.onDestroy();
    }
}